/**
 * 
 */
package cn.crm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;

import cn.crm.bean.User;
import cn.crm.bean.WorkPlan;
import cn.crm.dao.WorkPlanDao;
import cn.crm.service.WorkPlanService;

/**
 * @author Vi1___000
 *
 */
@Service("workPlanService")
public class WorkPlanServiceImpl implements WorkPlanService {
	
	@Resource
	private WorkPlanDao workPlanDao;

	/* (non-Javadoc)
	 * @see cn.crm.service.WorkPlanService#getAll(cn.crm.bean.WorkPlan)
	 */
	@Override
	public List<WorkPlan> getAll(WorkPlan plan) {
		// TODO Auto-generated method stub
		return workPlanDao.getAll(plan);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.WorkPlanService#getByID(int)
	 */
	@Override
	public WorkPlan getByID(int id) {
		// TODO Auto-generated method stub
		return workPlanDao.getByID(id);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.WorkPlanService#addWorkPlan(cn.crm.bean.WorkPlan)
	 */
	@Override
	public boolean addWorkPlan(WorkPlan plan) {
		// TODO Auto-generated method stub
		return workPlanDao.addWorkPlan(plan);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.WorkPlanService#updateWorkPlan(cn.crm.bean.WorkPlan)
	 */
	@Override
	public boolean updateWorkPlan(WorkPlan plan) {
		// TODO Auto-generated method stub
		return workPlanDao.updateWorkPlan(plan);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.WorkPlanService#deleteWorkPlan(cn.crm.bean.WorkPlan)
	 */
	@Override
	public boolean deleteWorkPlan(WorkPlan plan) {
		// TODO Auto-generated method stub
		WorkPlan temp = workPlanDao.getByID(plan.getWpPlanid());
		if(temp!=null){
			temp.setWpIsdelete(true);
			workPlanDao.updateWorkPlan(temp);
		}
		return false;
	}

	public void setWorkPlanDao(WorkPlanDao workPlanDao) {
		this.workPlanDao = workPlanDao;
	}

	@Override
	public List<WorkPlan> getByUserAndTime(User user, Date start, Date end) {
		// TODO Auto-generated method stub
		return workPlanDao.getByUserAndTime(user, start, end);
	}

	@Override
	public String getJSONByUserAndTime(User user, Date start, Date end) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		JSONArray jlist = new JSONArray();
		List<WorkPlan> list = workPlanDao.getByUserAndTime(user, start, end);
		for (WorkPlan workPlan : list) {
			JSONObject obj = new JSONObject();
			obj.put("id", workPlan.getWpPlanid());
			obj.put("title", workPlan.getWpTitle());
			obj.put("time", sdf.format(workPlan.getWpRemindedtime()));
//			obj.put("isok", workPlan.getDDLByTypeCompletion().getTypeValue());
			obj.put("live", workPlan.getDDLByTypeImportance().getTypeValue());
			jlist.add(obj);
		}
		System.out.println(jlist.toJSONString());
		return jlist.toJSONString();
	}

	@Override
	public String getJSONByUserAndOneDay(User user, Date tday) {
		// TODO Auto-generated method stub
		Date end = new Date(tday.getTime()+(3600*24*1000)-1);
		System.out.println(tday.toLocaleString());
		System.out.println(end.toLocaleString());
		return this.getJSONByUserAndTime(user, tday, end);
	}
	
	

}
